package cn.iotnova.rtc;

import android.content.Context;
import android.content.res.AssetManager;
import android.view.Surface;

import java.util.HashMap;
import java.util.concurrent.locks.Lock;

public class Rtcclient3 {
    static {
        System.loadLibrary("rtcws");
    }

    //ClientSignalInit

    public static native void ConnectToServer(String host);
    public static native void SendNV21Image(byte[] data, int width, int height, int size);
    public static native void SendNVRGB565Image(byte[] data, int width, int height);
    public static native void SendYUVImage(byte[] data, int width, int height);
    public static native void SendPCMData(byte[] data, int length);
    public static native void SendOpusData(byte[] data, int length);
    public native void Init(Surface surface, AssetManager assetManager);
    //public native void Draw(String path);
    public native void ShowYuv(byte[] dat, int len);
    public native void Destroyed();
    public static native void ClientSignalPeerInit(String host, String username, String passwd, String serial);
    //public static native void ClientSignalPeerInit(String hostname, String hostport, String hosturi, String username, String passwd, String serial);

    public static native void SendNV21Image2(byte[] data, int width, int height, int size);
    public static native void SendRGBAImage2(byte[] data, int width, int height, int size);



    ///////////////////////////////////////////////////////////////////////////////////////////

    public void Init(Context context) {
        activity = context;
        new Thread(new Runnable() {
            @Override
            public void run() {
                String conn_id;
                while (true) {
                    conn_id = ClientSignalInit("192.168.1.2", "7000", "clientws", "zyh", "123", "1");
                    //conn_id = ClientSignalInit("114.117.194.15", "7000", "clientws", "zyh", "123", "1");
                    //Websocket.setId(connId);
                    connId = conn_id;

                    ClientReadData(conn_id);
                }
            }
        }).start();
    }

    static Context activity;

    public static ISignalcallback signalcallback = null;

    public static void _Init(Context context) {
        activity = context;
        new Thread(new Runnable() {
            @Override
            public void run() {
                String connId;
                while (true) {
                    //connId = ClientSignalInit("114.117.194.15", "7000", "clientws", "zyh", "123", "124");
                    connId = ClientSignalInit("192.168.1.2", "7000", "clientws", "zyh", "123", "124");
                    //Websocket.setId(connId);
                    id = connId;

                    ClientReadData(connId);
                    callbackMap.remove(connId);
                }
            }
        }).start();
    }

    public static void Remove(String id) {
        callbackMap.remove(id);
    }

    public static HashMap<String, ISignalcallback> callbackMap = new HashMap<String, ISignalcallback>();


    private static String id;

    private String connId;

    public void setId(String _id) {
        connId = _id;
    }

    public static void setCallback(String id, ISignalcallback signalcallback) {
        callbackMap.put(id, signalcallback);
    }

    ///////////////////////////////////////////////////////////////////////////////////
    public static void SocketRecv(String id, int type, String dat) {
        //activity.handleMessage(dat);
        //signalcallback.SignalRecv("", dat);
        callbackMap.get(id).SignalRecv(type, dat);
    }
    public static native String ClientSignalInit(String hostname, String hostport, String hosturi, String username, String passwd, String serial);
    public static native void ClientReadData(String id);
    public static native void ClientSendData(String id, int type, String dat);
    private static native void ClientClose(String id);
    public static void Close(String id) {
        lock.lock();
        ClientClose(id);
        Remove(id);
        lock.unlock();
    }
    public static Lock lock;
}
